package com.netdisk.controller;

import static com.netdisk.util.IcoReader.getIcos;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.netdisk.service.impl.FileSystemService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 下载文件拦截器
 * @author chenlong
 * 2013-5-16
 */
@Component("downloadFileAction")
@Scope("prototype")
public class DownloadFileAction extends ActionSupport{
	
	private FileSystemService fileSystemService;
	
	private String id; // 文件系统的id； 
	
	private String fileName; // 文件的真实名称
	
	private String storeName; // 文件的保存的名称UUid
	
	private String storeDir;// 文件存储路径Uuid,

	public String getStoreName() {
		return storeName;
	}

	public void setStoreName(String storeName) {
		this.storeName = storeName;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String execute() throws Exception {
		 return SUCCESS;
	}
	
	public String getFileName() {
		return fileName;
	}
	
	public InputStream getInputStream() throws FileNotFoundException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession	session =  request.getSession();
		String contextpath = session.getServletContext().getRealPath("WEB-INF/filesystem");
		return new FileInputStream(contextpath + File.separator + storeDir + File.separator + storeName);
	}


	public String getStoreDir() {
		return storeDir;
	}

	public void setStoreDir(String storeDir) {
		this.storeDir = storeDir;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	@Resource(name="fileSystemService")
	public void setFileSystemService(FileSystemService fileSystemService) {
		this.fileSystemService = fileSystemService;
	}
}
